package com.earthquake.monitor_system.service.impl;

import com.earthquake.monitor_system.controller.resultType.IconType;
import com.earthquake.monitor_system.dao.IconClassifyDao;
import com.earthquake.monitor_system.dao.IconDao;
import com.earthquake.monitor_system.dao.SysMenuDao;
import com.earthquake.monitor_system.domain.IconClassifyDomain;
import com.earthquake.monitor_system.domain.IconDomain;
import com.earthquake.monitor_system.domain.SysMenuDomain;
import com.earthquake.monitor_system.domain.UserMenuDomain;
import com.earthquake.monitor_system.service.ITF.ISysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysMenuImp implements ISysMenu {
    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
    private IconDao iconDao;
    @Autowired
    private IconClassifyDao iconClassifyDao;
    @Override
    public  List<SysMenuDomain> getMenuList() {
        List<SysMenuDomain> parentMenu = sysMenuDao.getParentMenu();
        for (SysMenuDomain menu : parentMenu) {
            Integer menuId = menu.getId();
            List<SysMenuDomain> childrenMenu = sysMenuDao.getChildrenMenu(menuId);
            menu.setChildren(childrenMenu);
        }
        return parentMenu;
    }

    @Override
    public List<SysMenuDomain> getUserMenuList(Integer id) {
        List<UserMenuDomain> userMenu = sysMenuDao.getUserMenu(id);
        List<SysMenuDomain> menuList = new ArrayList<>();
        userMenu.forEach(item -> {
            Integer sysMenuId = item.getSys_menu_id();
            SysMenuDomain sysMenu = sysMenuDao.getSysMenu(sysMenuId);
            menuList.add(sysMenu);
        });
        menuList.forEach(item -> {
            if (item.getPid()==null) {
                List<SysMenuDomain> children = menuList.stream().filter(s -> s.getPid() == item.getId()).collect(Collectors.toList());
                item.setChildren(children);
            }
        });
        return menuList.stream().filter(item -> item.getPid()==null).collect(Collectors.toList());
    }

    @Override
    public List<IconType> getIcon() {
        List<IconType> result = new ArrayList<>();
        List<IconClassifyDomain> IconClassifyAll = iconClassifyDao.getAll();
        IconClassifyAll.forEach(item -> {
            IconType iconType = new IconType();
            iconType.setLabel(item.getClassification());
            IconClassifyDomain one = iconClassifyDao.getOne(item.getClassification());
            Integer ClassifyId = one.getId();
            List<IconDomain> IconList = iconDao.getByIdAll(ClassifyId);
            iconType.setOptions(IconList);
            result.add(iconType);
        });
        return result;
    }

    @Override
    public HashMap<String,Object> insetParentMenu(SysMenuDomain sysMenuDomain) {
        SysMenuDomain one = sysMenuDao.getOne(sysMenuDomain.getName());
        HashMap<String,Object> map = new HashMap<>();
        if(one!=null) {
            map.put("isExist",true);
            map.put("msg","菜单已存在！请更换菜单名！");
            return map;
        }
        Boolean aBoolean = sysMenuDao.insetParentMenu(sysMenuDomain);
        map.put("isExist",false);
        map.put("isInsert",aBoolean);
        return map;
    }

    @Override
    public HashMap<String, Object> insetChildrenMenu(SysMenuDomain sysMenuDomain) {
        SysMenuDomain one = sysMenuDao.getChildrenOne(sysMenuDomain.getName());
        HashMap<String,Object> map = new HashMap<>();
        if(one!=null) {
            map.put("isExist",true);
            map.put("msg","菜单已存在！请更换菜单名！");
            return map;
        }
        Boolean aBoolean = sysMenuDao.insetChildrenMenu(sysMenuDomain);
        map.put("isExist",false);
        map.put("isInsert",aBoolean);
        return map;
    }

    @Override
    public Boolean updateMenu(SysMenuDomain sysMenuDomain) {
        Boolean aBoolean = sysMenuDao.updateMenu(sysMenuDomain);
        return aBoolean;
    }

    @Override
    public Boolean DeleteMenu(Integer id) {
        Boolean aBoolean = sysMenuDao.DeleteMenu(id);
        return aBoolean;
    }

    @Override
    public Boolean userMenu(Integer id, List menuList) {
        if(menuList.size()==0) {
            Boolean aBoolean1 = sysMenuDao.deleteUserMenu(id);
            return  true;
        }
        List<UserMenuDomain> userMenu = sysMenuDao.getUserMenu(id);
        if(userMenu.size()==0) {
            Boolean aBoolean = sysMenuDao.userMenu(id, menuList);
            return aBoolean;
        }else {
            Boolean aBoolean1 = sysMenuDao.deleteUserMenu(id);
            Boolean aBoolean = sysMenuDao.userMenu(id, menuList);
            return aBoolean;
        }
    }

    @Override
    public List<Integer> getUserMenu(Integer userId) {
        List<UserMenuDomain> userMenu = sysMenuDao.getUserMenu(userId);
        List<Integer> list = new ArrayList<>();
        userMenu.forEach(item -> {
            Integer sysMenuId = item.getSys_menu_id();
            list.add(sysMenuId);
        });
        return list;
    }
}
